<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use Exception;
use fast\Random;
use think\exception\PDOException;

/**
 * 答题卡设计
 */
class Card extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //答题卡列表
    public function cardList()
    {
        if (!$this->request->isPost()) $this->error('请求方式错误');
        $name = $this->request->post('name');
        $self = $this->request->post('self', 0);
        $page = $this->request->post('page', 1);
        $pagesize = $this->request->post('pagesize', 10);
        $school_id = $this->school_id;
        $user_id = $this->user_id;
        $list = db('answer_card')
            ->where(['school_id' => $school_id])
            ->where(function ($query) use ($self, $user_id) {
                if ($self) {
                    $query->where('user_id', $user_id);
                }
            })
            ->where(function ($query) use ($name) {
                if ($name) {
                    $query->where('name', 'like', '%' . $name . '%');
                }
            })
            ->paginate($pagesize, false)->each(function ($item, $index) {
                $item['creattime_text'] = date('Y-m-d H:i:s', $item['creattime']);
                return $item;
            });
        $this->success('答题卡列表', $list);
    }

    //答题卡详情
    public function cardInfo()
    {
        if (!$this->request->isPost()) $this->error('请求方式错误');
        $id = $this->request->post('id');
        if (!$id) $this->error('未知参数！');
        $info = db('answer_card')->where(['id' => $id])->find();
        if ($info) {
            $info['schoolName'] = db('school')->where('id',$info['school_id'])->value('name');
            $info['userName'] = db('user')->where('id',$info['user_id'])->value('nickname');
        }
        $this->success('答题卡详情', $info);
    }

    //添加答题卡
    public function addCard()
    {
        if (!$this->request->isPost()) $this->error('请求方式错误');
        $name = $this->request->post('name');
        $paperType = $this->request->post('paperType');
        $fenceType = $this->request->post('fenceType');
        $score = $this->request->post('score');
        if (!$name || !$paperType || !$fenceType || !$score) $this->error('未知参数！');
        $params = $this->request->post('params/a');
        if (!is_array($params) && empty($params)) $this->error('未知参数！');
        $school_id = $this->school_id;
        $user_id = $this->user_id;
        $in = db('answer_card')->insertGetId([
            'school_id' => $school_id,
            'name' => $name,
            'user_id' => $user_id,
            'paperType' => $paperType,
            'fenceType' => $fenceType,
            'score' => $score,
            'params' => json_encode($params),
            'creattime' => time()
        ]);
        if ($in) {
            $this->success('添加成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //复制答题卡
    public function copyCard()
    {
        if (!$this->request->isPost()) $this->error('请求方式错误');
        $id = $this->request->post('id');
        if (!$id) $this->error('未知参数！');
        $info = db('answer_card')->where(['id' => $id])->find();
        if (!$info) $this->error('未找到选择的答题卡模版信息！');
        $in = db('answer_card')->insertGetId([
            'school_id' => $info['school_id'],
            'name' => $info['name'],
            'user_id' => $info['user_id'],
            'paperType' => $info['paperType'],
            'fenceType' => $info['fenceType'],
            'score' => $info['score'],
            'params' => $info['params'],
            'creattime' => time()
        ]);
        if ($in) {
            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //复制答题卡
    public function delCard()
    {
        if (!$this->request->isPost()) $this->error('请求方式错误');
        $ids = $this->request->post('ids');
        if (!$ids) $this->error('未知参数！');
        $ids = explode(',',$ids);
        $del = db('answer_card')->where('id','in',$ids)->delete();
        if ($del) {
            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }
}
